// JavaScript Document
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
(function($){
	$.fn.show_city = function(){
		
		var cur_obj = null;
		var down_list = null;	
		var $input_provice = null;
		var $input_city = null;
		var lst_provice = null;
		var lst_city = null;
		var shadow_id = "";
		var cur_lst = null;
		
		return this.each(function(){						  
			var $this = $(this);
			
			if($("#lst_city").length == 0){
				$("body").append('<div id="lst_provice" class="lst_city"></div><div id="lst_city" class="lst_city"></div>');			
			}
			lst_provice = $Q("#lst_provice");
			lst_city = $Q("#lst_city");
			$this.focus(function(){									 
				cur_obj = $(this);				
				$input_provice = $(this).siblings(":hidden[name='txt_provice']");
				$input_city = $input_provice.next(":hidden");
				write_provice();
				show_lst(lst_provice);		
		
				//页面点击事件, 根据点击对象决定是否隐藏列表
				$(document).click(function(e){				
					e = e ? e : window.event;
					var src = e.srcElement ? e.srcElement : e.target;	
					
					var src_class = $(src).parent("p").parent().attr("class")||$(src).attr("class");
					var cur_class = cur_lst.attr("class");
					var src_name = $(src).attr("name");
					var cur_name = cur_obj.attr("name");
					//alert(src_name==cur_name)
					if(src_class==cur_class||src_name==cur_name){				
						return true;							
					}	
					hide_lst(cur_lst);						
				})
			})
		})		
				
		function write_provice(){	
			var lst = get_area();	
			var htm = "";
			for(i=0;i<lst.length;i++){
				htm += '<p>';
				htm += '<span>'+lst[i]+':</span>';
				var lst1 = get_provice(i);
				for(j=0;j<lst1.length;j++){
					var x = String(i) + String(j);
					var cls ="";
					//if(j==6){
//					cls = ' class="mgn"';
//				}
					var str1=lst1[j];
					var str="";
					var h="";
					if(str1.indexOf('.')!=-1){
						str=str1.substring(0,str1.indexOf('.'));	
						h=str1.substring(str1.indexOf('.')+1);	
					}else{
						str=str1;						
					}
				
				if(i==0||i==7){	
			
					htm += '<a'+cls+' href="javascript:void(0)" h="'+h+'" >'+str+'</a>';							
				}else{
				
					htm += '<a'+cls+' href="javascript:void(0)" xid="'+x+'" h="'+h+'">'+str+'</a>';
				}
				//if(j==5){
//					htm += '<br />';
//				}
			}
			htm +='</p>';
			}	
			lst_provice.html(htm);
			hide_lst(lst_city);
			//添加省份点击事件
			lst_provice.find("a").click(function(){
				cur_obj.val($(this).text())
				$input_provice.val($(this).text());	
				$input_city.val("");			
				if($(this).attr("xid")){
					write_city($(this).attr("xid"));
				}else{
					$("#jiguan").val($(this).attr("h"));
					hide_lst(lst_provice);
				}
			})			
		}
		
		function write_city(idx){			
			var lst = get_city(String(idx));	
			var htm = '<p><a href="javascript:void(0)" class="a_back">&laquo; 返回省份列表</a></p>' ;
			htm += '<p>'
			for(i=0;i<lst.length;i++){	
				var str1=lst[i];
				var str="";
				var h="";
				if(str1.indexOf('.')!=-1){
					str=str1.substring(0,str1.indexOf('.'));	
					h=str1.substring(str1.indexOf('.')+1);	
				}else{
					str=str1;						
				}
				htm += '<a href="javascript:void(0)"  h="'+h+'">'+str+'</a>';
			}
			htm += '</p>';			
			lst_city.html(htm);				
			hide_lst(lst_provice);
			//添加城市点击事件			
			lst_city.find("a").click(function(){				
				hide_lst(lst_city);
				if($(this).attr("class")=="a_back"){
					show_lst(lst_provice);
				}else{
					var old_val = cur_obj.val();
					var txt = $(this).text();
					$("#jiguan").val($(this).attr("h"));
					cur_obj.val(old_val+txt);	
					$input_city.val(txt);					
				}				
			})
			show_lst(lst_city);	
		}
		function show_lst(lst){
			cur_lst = lst;				
			var ofs = get_offset();
			var l = ofs.left;
			var t = ofs.bottom;		
			lst.css({left:l,top:t}).show();				
			create_shadow(cur_lst);			
		}
		function hide_lst(obj){			
			obj.hide();	
			$(".bg_shadow").remove();			
		}
		function get_offset(){
			var ofs = cur_obj.offset();
			var l = ofs.left;			
			var pt = parseInt(cur_obj.css("padding-top"));
			var pb = parseInt(cur_obj.css("padding-bottom"));
			var bt = parseInt(cur_obj.css("border-top-width")) ? parseInt(cur_obj.css("border-top-width")):0;	
			var bb = parseInt(cur_obj.css("border-bottom-width")) ? parseInt(cur_obj.css("border-bottom-width")):0;
			var b = cur_obj.height() + ofs.top + bt + bb + pt + pb;
			return {
				left	: l,
				bottom	: b
			};
		}
		function create_shadow(obj){
			if($(".bg_shadow").length == 0){
				obj.after('<div class="bg_shadow"></div>');
			}
			var w = obj.width()+12;			
			var h = obj.height()+12;
			
			var l = obj.offset().left + 2;
			var t = obj.offset().top + 2;			
			
			$(".bg_shadow")
				.css({"width":w+"px","height":h+"px","left":l+"px","top":t+"px"})
				.show();
				//.bgiframe();
		}
		//地区
		function get_area(){
			 return new Array("直辖市","华北","东北","华东","中南","西南","西北","港澳台");
		}	
		
		//省份
		function get_provice(idx){
			switch(idx){
				case 0:
					return new Array('北京市.110000','天津市.120000','上海市.310000','重庆市.500000');
				case 1:
					return new Array('河北省.130000','山西省.140000','内蒙古自治区.150000');
				case 2:
					return new Array('辽宁省.210000','吉林省.220000','黑龙江省.230000');
				case 3:
					return new Array('江苏省.320000','浙江省.330000','安徽省.340000','福建省.350000','江西省.360000','山东省.370000');
				case 4:
					return new Array('河南省.410000','湖北省.420000','湖南省.430000','广东省.440000','广西壮族自治区.450000','海南省.460000');					
				case 5:
					return new Array('四川省.510000','贵州省.520000','云南省.530000','西藏自治区.540000');
				case 6:
					return new Array('陕西省.610000','甘肃省.620000','青海省.630000','宁夏回族自治区.640000','新疆维吾尔自治区.650000');
				case 7:
					return new Array('香港特别行政区.810000','澳门特别行政区.820000','台湾省.710000');
				default:
					return new Array();
			}
		}
		//城市
		function get_city(idx){
			switch(idx){
				case "10":
					return new Array('石家庄市.130100','唐山市.130200','秦皇岛市.130300','邯郸市.130400','邢台市.130500','保定市.130600','张家口市.130700','承德市.130800','沧州市.130900','廊坊市.131000','衡水市.131100');
				case "11":
					return new Array('太原市.140100','大同市.140200','阳泉市.140300','长治市.140400','晋城市.140500','朔州市.140600','晋中市.140700','运城市.140800','忻州市.140900','临汾市.141000','吕梁市.142300');
				case "12":
					return new Array('呼和浩特市.150100','包头市.150200','乌海市.150300','赤峰市.150400','通辽市.150500','鄂尔多斯市.150600','呼伦贝尔市.150700','巴彦淖尔市.152800','乌兰察布市.152600','锡林郭勒盟.152500','兴安盟.152200','阿拉善盟.152900');					
				case "20":
					return new Array('沈阳市.210100','大连市.210200','鞍山市.210300','抚顺市.210400','本溪市.210500','丹东市.210600','锦州市.210700','营口市.210800','阜新市.210900','辽阳市.211000','盘锦市.211100','铁岭市.211200','朝阳市.211300','葫芦岛市.211400');
				case "21":
					return new Array('长春市.220100','吉林市.220200','四平市.220300','辽源市.220400','通化市.220500','白山市.220600','松原市.220700','白城市.220800','延边朝鲜族自治州.220200');
				case "22":
					return new Array('哈尔滨市.230100','齐齐哈尔市.230200','鹤岗市.230400','双鸭山市.230500','鸡西市.230300','大庆市.230600','伊春市.230700','牡丹江市.231000','佳木斯市.230800','七台河市.230900','黑河市.231100','绥化市.231200','大兴安岭.232700');
				case "30":
					return new Array('南京市.320100','无锡市.320200','徐州市.320300','常州市.320400','苏州市.320500','南通市.320600','连云港市.320700','淮安市.320800','盐城市.320900','扬州市.321000','镇江市.321100','泰州市.321100','宿迁市.321300');
				case "31":
					return new Array('杭州市.330100','宁波市.330200','温州市.330300','嘉兴市.330400','湖州市.330500','绍兴市.330600','金华市.330700','衢州市.330800','舟山市.330900','台州市.331000','丽水市.331100');
				case "32":
					return new Array('合肥市.340100','芜湖市.340100','蚌埠市.340300','淮南市.340400','马鞍山市.340500','淮北市.340600','铜陵市.340700','安庆市.340800','黄山市.341000','滁州市.341100','阜阳市.341200','宿州市.341300','巢湖市.341400','六安市.341500','亳州市.341500','池州市.341700','宣城市.341800');
				case "33":
					return new Array('福州市.350100','厦门市.350200','莆田市.350300','三明市.350400','泉州市.350500','漳州市.350600','南平市.350700','龙岩市.350800','宁德市.350900');
				case "34":
					return new Array('南昌市.360100','景德镇市.360200','萍乡市.360300','九江市.360400','新余市.360500','鹰潭市.360600','赣州市.360700','吉安市.360800','宜春市.360900','抚州市.361000','上饶市.361100');
				case "35":
					return new Array('济南市.361100','青岛市.370200','淄博市.370300','枣庄市.370400','东营市.370500','烟台市.370600','潍坊市.370700','济宁市.370800','泰安市.370900','威海市.371000','日照市.371100','莱芜市.371200','临沂市.371300','德州市.371400','聊城市.371500','滨州市.371600','菏泽市.371700');
				case "40":
					return new Array('郑州市.410100','开封市.410200','洛阳市.410300','平顶山市.410400','安阳市.410500','鹤壁市.410600','新乡市.410700','焦作市.410800','濮阳市.410900','许昌市.411000','漯河市.411100','三门峡市.411200','南阳市.411300','商丘市.411400','信阳市.411500','周口市.411600','驻马店市.411700','济源市.411800');
				case "41":
					return new Array('武汉市.420100','黄石市.420200','十堰市.420300','荆州市.421000','宜昌市.420500','襄樊市.420600','鄂州市.420700','荆门市.420800','孝感市.420900','黄冈市.421100','咸宁市.421200','随州市.421300','恩施土家族.422800');			
				case "42":
					return new Array('长沙市.430100','株洲市.430200','湘潭市.430300','衡阳市.430400','邵阳市.430500','岳阳市.430600','常德市.430700','张家界市.430800','益阳市.430900','郴州市.431000','永州市.431100','怀化市.431200','娄底市.431300','湘西土家族.433100');	
				case "43":
					return new Array('广州市.440100','深圳市.440300','珠海市.440400','汕头市.440500','韶关市.440200','佛山市.440600','江门市.440700','湛江市.440800','茂名市.440900','肇庆市.441200','惠州市.441300','梅州市.441400','汕尾市.441500','河源市.441600','阳江市.441700','清远市.441800','东莞市.441900','中山市.442000','潮州市.445100','揭阳市.445200','云浮市.445300');			
				case "44":
					return new Array('南宁市.450100','柳州市.450200','桂林市.450300','梧州市.450400','北海市.450500','防城港市.450600','钦州市.450700','贵港市.450800','玉林市.450900','百色市.451000','贺州市.451100','河池市.451200','南宁地区.452100','柳州地区.452200');		
				case "45":
					return new Array('海口市.460100','三亚市.460200','五指山市.469001','琼海市.469002','儋州市.469003','文昌市.469005','万宁市.469006','东方市.469007','澄迈县.469023','定安县.469021','屯昌县.469022','临高县.469024','白沙黎族.469025','昌江黎族.469026','乐东黎族.469027','陵水黎族.469028','保亭黎族苗族.469029','琼中黎族苗族.469030');
				case "50":
					return new Array('成都市.510100','自贡市.510300','攀枝花市.510400','泸州市.510500','德阳市.510600','绵阳市.510700','广元市.510800','遂宁市.510900','内江市.511000','乐山市.511100','南充市.511300','眉山市.511400','宜宾市.511500','广安市.511600','达州市.511700','雅安市.511800','巴中市.511900','资阳市.512000','阿坝藏族羌族.513200','甘孜藏族.513300','凉山彝族.513400');			
				case "51":
					return new Array('贵阳市.520100','六盘水市.520200','遵义市.520300','安顺市.520400','铜仁地区.522200','毕节地区.522400','黔西南布依族.522300','黔东南苗族侗族.522600','黔南布依族.522700');
				case "52":
					return new Array('昆明市.530100','曲靖市.530300','玉溪市.530400','保山市.530500','昭通市.530600','丽江市.533200','思茅市.532700','临沧市.533500','文山壮族苗族.532600','红河哈尼族彝族.532500','西双版纳傣族.532800','楚雄彝族.532300','大理白族.532900','德宏傣族景颇族.533100','怒江傈傈族.533300','迪庆藏族.533400');
				case "53":
					return new Array('拉萨市.540100','那曲地区.542400','昌都地区.542100','山南地区.542200','日喀则地区.542300','阿里地区.542500','林芝地区.542600');		
				case "60":
					return new Array('西安市.610100','铜川市.610200','宝鸡市.610300','咸阳市.610400' ,'渭南市.610500','延安市.610600','汉中市.610700','榆林市.610800','安康市.610900','商洛市.611000');
				case "61":
					return new Array('兰州市.620100','金昌市.620300','白银市.620400','天水市.620500','嘉峪关市.620200','武威市.620600','张掖市.620700','平凉市.620800','酒泉市.620900','庆阳市.621000','定西市.622400','陇南市.622600','临夏回族.622900','甘南藏族.623000');			
				case "62":
					return new Array('西宁市.630100','海东地区.632100','海北藏族.632200','黄南藏族.632300','海南藏族.632500','果洛藏族.632600','玉树藏族.632700','海西蒙古族.632800');			
				case "63":
					return new Array('银川市.640100','石嘴山市.640200','吴忠市.640300','固原市.640400');
				case "64":
					return new Array('乌鲁木齐市.650100','克拉玛依市.650200','克孜勒苏柯尔克孜自治州.653000','吐鲁番市.652100','阿克苏市.652900','喀什市.653100','哈密市.652200','和田市.653200','昌吉市.652300','博乐市.652700','伊宁市.654000','塔城市.654200','省直辖行政单位.659000','阿勒泰市.654300','巴音郭楞蒙古自治州.652800');						
				default:
					return new Array();
			}
		}		
	}
})(jQuery);
$Q(function(){ 
	$Q(":text[typx='city']").show_city();
});